<?php
namespace payment\service;
use api\utils\BankAPIClient;
use herosphp\core\Loader;
use herosphp\core\Log;
use herosphp\string\StringUtils;
use herosphp\utils\ModelTransformUtils;
use payment\models\QueryAccountVo;
use payment\models\SignVo;
use payment\models\TradeDetailVo;
use payment\models\TransferVo;
use payment\models\WithdrawVo;
use payment\service\interfaces\IPaymentService;

Loader::import('payment.service.interfaces.IPaymentService');
/**
 * 支付网关服务接口实现
 * @package payment\service\interfaces
 * @author yangjian<yangjian102621@gmail.com>
 * @since 2016-11-16
 */
class PaymentService implements IPaymentService {

    /**
     * 签约，解约
     * @param SignVo $signVo
     * @return mixed
     */
    public function sign(SignVo $signVo) {
        //把Vo转为map
        $params = ModelTransformUtils::model2Map($signVo);
        if ( $signVo->getType() == SignVo::SIGN ) {
            $url = 'payment/sign';
            $opt = '签约';
        } else {
            $opt = '解约';
            $url = 'payment/unsign';
        }
        $res = BankAPIClient::send($url, $params);
        if ( $res->isSucess() ) {
            return $res->getData();
        } else {
            //记录错误日志
            $data = $res->getData();
            Log::info("{$opt}失败, 错误代码： {$data['message']}, {$res->getMessage()}");
            Log::info(StringUtils::jsonEncode($params));
            return false;
        }
    }

    /**
     * 入金
     * @param TransferVo $transferVo
     * @return mixed
     */
    public function inPay(TransferVo $transferVo) {
        //把Vo转为map
        $params = ModelTransformUtils::model2Map($transferVo);
        $res = BankAPIClient::send('payment/transfer', $params);
        if ( $res->isSucess() ) {
            return $res->getData();
        } else {
            //记录错误日志
            $data = $res->getData();
            Log::info("入金失败, 错误代码： {$data['message']}, {$res->getMessage()}");
            Log::info(StringUtils::jsonEncode($params));
            return false;
        }
    }

    /**
     * 出金
     * @param TransferVo $transferVo
     * @return mixed
     */
    public function outPay(TransferVo $transferVo) {
        //把Vo转为map
        $params = ModelTransformUtils::model2Map($transferVo);
        $res = BankAPIClient::send('payment/transfer', $params);
        if ( $res->isSucess() ) {
            return $res->getData();
        } else {
            //记录错误日志
            $data = $res->getData();
            Log::info("出金失败, 错误代码： {$data['message']}, {$res->getMessage()}");
            Log::info(StringUtils::jsonEncode($params));
            return false;
        }
    }

    /**
     * 查询余额
     * @param QueryAccountVo $queryAccountVo
     * @return mixed
     */
    public function getBalance(QueryAccountVo $queryAccountVo) {
        //把Vo转为map
        $params = ModelTransformUtils::model2Map($queryAccountVo);
        $res = BankAPIClient::send('payment/queryaccount', $params);
        if ( $res->isSucess() ) {
            $item = $res->getData();
            return $item['balance'];
        } else {
            //记录错误日志
            $data = $res->getData();
            Log::info("查询失败, 错误代码： {$data['message']}, {$res->getMessage()}");
            Log::info(StringUtils::jsonEncode($params));
            return false;
        }
    }

    /**
     * 提现
     * @param WithdrawVo $withdrawVo
     * @return mixed
     */
    public function withdraw(WithdrawVo $withdrawVo) {
        //把Vo转为map
        $params = ModelTransformUtils::model2Map($withdrawVo);
        $res = BankAPIClient::send('payment/withdraw', $params);
        if ( $res->isSucess() ) {
            return $res->getData();
        } else {
            //记录错误日志
            $data = $res->getData();
            Log::info("提现失败, 错误代码： {$data['message']}, {$res->getMessage()}");
            Log::info(StringUtils::jsonEncode($params));
            return false;
        }
    }

    /**
     * 批量查询附属账户流水
     * @param TradeDetailVo $detailVo
     * @return mixed
     */
    public function tradedetail(TradeDetailVo $detailVo)
    {
        //把Vo转为map
        $params = ModelTransformUtils::model2Map($detailVo);
        $res = BankAPIClient::send('payment/tradedetail', $params);
        if ( $res->isSucess() ) {
            return $res->getData();
        } else {
            //记录错误日志
            $data = $res->getData();
            Log::info("查询流水失败, 错误代码： {$data['message']}, {$res->getMessage()}");
            Log::info(StringUtils::jsonEncode($params));
            return false;
        }
    }
}
